package com.example.userpointmgr.service;

import com.example.userpointmgr.dao.*;
import com.example.userpointmgr.map.ReviewMapper;
import com.example.userpointmgr.map.UserMapper;
import com.example.userpointmgr.tool.PageParam;
import com.example.userpointmgr.tool.PageResult;
import com.example.userpointmgr.tool.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ReviewServiceImpl implements ReviewService{
    @Autowired
    private ReviewMapper reviewMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult<ReviewPo> findAllUsers(PageParam pageParam) {
        //获取总数
        int total=reviewMapper.total();
        List<ReviewPo> userPoList = reviewMapper.findAllUsers(pageParam.getOffset(), pageParam.getPageSize());
        return new  PageResult<>(total,userPoList);
    }
    @Override
    public ResponseResult updateReview(ReviewDto reviewDto) {
        int A=reviewMapper.updateReviewById(reviewDto);
        if(A!=1){
            return ResponseResult.fail("暂无数据");
        }else {
            if (reviewDto.getIsToPass()==1){
                UserPo user=userMapper.selectOneByPhoneNumber(reviewDto.getPhoneNumber());
                int a=user.getUserPoints();
                int b= Integer.parseInt(reviewDto.getContent());
                int c=a+b;
                reviewDto.setId(user.getUserPoints());
                reviewDto.setContent(String.valueOf(c));
                userMapper.updateUserByPhone(reviewDto);
                return ResponseResult.success(A);
            }else {
                return ResponseResult.success(A);
            }
        }
    }

    @Override
    public ResponseResult addReview(ReviewAddDto reviewAddDto) {
//        被操作人
        UserPo userPo = userMapper.selectOneByOpenid(reviewAddDto.getOpenid());
//        操作人
        UserPo user=userMapper.selectOneByOpenid(reviewAddDto.getOpenidcreate());
        ReviewPo reviewPo = new ReviewPo();
        reviewAddDto.setContent("0");
        reviewPo.setCreateTime(new Date());
        reviewPo.setIsToPass(0);
        reviewPo.setIsReport(0);
        reviewPo.setCreateUser(user.getRealName());
        reviewPo.setUserName(userPo.getRealName());
        reviewPo.setPhoneNumber(userPo.getPhoneNumber());
        reviewPo.setReason(reviewAddDto.getReason());
        reviewPo.setContent(reviewAddDto.getContent());
        return ResponseResult.success(reviewMapper.reviewAdd(reviewPo));
    }

    public ResponseResult updatePoint(ConfirmDto confirmDto) {
        return ResponseResult.success(reviewMapper.updatePoint(confirmDto));
    }
}
